package cfengMiddleware.server.rabbitmq.consumer;

import cfengMiddleware.server.rabbitmq.entity.MessageEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author Cfeng
 * @date 2022/9/23
 * 主要主动监听，不会自动推送，实现Runner、Ordered
 */

@Component
@RequiredArgsConstructor
@Slf4j
public class RedissonQueueConsumer implements ApplicationRunner, Ordered {

    private final RedissonClient redissonClient;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //队列组件的名称
        final String key = "redissonBasicQueue";
        //获取该组件
        RQueue<MessageEntity> rQueue = redissonClient.getQueue(key);

        while (true) {
            //这里需要一直运行,从队列弹出消息
            MessageEntity msg = rQueue.poll();

            if(!Objects.isNull(msg)) {
                log.info("监听到消息,{}",msg);
                //处理消息
            }
        }
    }

    @Override
    public int getOrder() {
        return -1;
    }
}
